package perso.marcr.lst.cron;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;

import perso.marcr.lst.model.Configuration;
import perso.marcr.lst.service.ConfigurationService;
import perso.marcr.lst.service.TaskService;
import perso.marcr.lst.service.impl.ConfigurationServiceImpl;
import perso.marcr.lst.service.impl.TaskServiceImpl;
import perso.marcr.util.Date;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class LaunchTaskCron implements Job
{
	// Note: Quartz does not support Autowired.	
	private ConfigurationService configurationService = new ConfigurationServiceImpl();

	private TaskService taskService = new TaskServiceImpl();



	public LaunchTaskCron()
	{
		System.out.println("LOADING LaunchTaskCron");
	}

	public void execute(JobExecutionContext context) throws JobExecutionException {		


		JobDataMap jdm = context.getJobDetail().getJobDataMap();
		String szJobName = (String) jdm.get("jobName");

		try {
			// load configuration :		
			Configuration configuration = configurationService.load(szJobName);

			// execute tests:
			String szTaskId = Date.getNow();
			jdm.put("taskId", szTaskId);
			taskService.launchTest(szJobName, szTaskId, configuration);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}	

}
